<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css//login.css">
</head>
<body>
    <div class="nav">
        五子棋
    </div>
    <div class="login-container">
        <!-- 登录页面对话框 -->
        <div class="login-dialog">
            <h3>登录</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="name">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="register">
                没有账号？<a href="register.html">点击注册</a>
            </div>
            <div class="row">
                <button id="submit">登录</button>
            </div>
        </div>
    </div>
    <script src="/js/jquery.min.js"></script>
    <script>
        let submitButton = document.querySelector('#submit'); // 提交按钮
        // 添加点击事件
        submitButton.onclick = function() { 
            let name =  $("#name").val();
            let password = $("#password").val();
            if (name == "") {
                alert("用户名不能为空");
                return;
            }
            if (null == "") {
                alert("用户密码不能为空");
                return;
            }
            $.ajax({
                url: '/login',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    name: name,
                    password: password,
                }),
                success: function(result) {
                    console.log(result);
                    if(result.code == 200) {
                        // 保存 token 信息
                        localStorage.setItem("user_token", result.data.token);
                        // 登录成功，跳转至游戏大厅
                        location.assign('/game_hall.html'); 
                    }else {
                        alert(result.errorMessage);
                    }
                }, error: function() {
                    alert("登录失败!");
                }
            })
        }
    </script>
</body>
</html>